//
//  SYDReturnScheduleViewController.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/8.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDReturnScheduleViewController.h"
#import "SYDReturnScheduleCell.h"
#define scheduleID @"SYDReturnScheduleCell"

@interface SYDReturnScheduleViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *tableHeaderView;
@property (weak, nonatomic) IBOutlet UILabel *categaryLbl;
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (weak, nonatomic) IBOutlet UILabel *scheduleStatus;
@property (weak, nonatomic) IBOutlet UIImageView *statusImageView;
@property (weak, nonatomic) IBOutlet UILabel *leftStatus;
@property (weak, nonatomic) IBOutlet UILabel *leftTime;
@property (weak, nonatomic) IBOutlet UILabel *middleStatus;
@property (weak, nonatomic) IBOutlet UILabel *middleTime;
@property (weak, nonatomic) IBOutlet UILabel *rightStatus;
@property (weak, nonatomic) IBOutlet UILabel *rightLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation SYDReturnScheduleViewController
#pragma mark - 导航栏
- (void)navigationBarHandle{
    
    WEAKSELF
    [weakSelf backButtonAction:^{
        
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationBar.title = @"提现进度";
    [self navigationBarHandle];
    [self initBankCardUI];
}
-(void)initBankCardUI{
    [self.tableView registerNib:[UINib nibWithNibName:scheduleID bundle:nil] forCellReuseIdentifier:scheduleID];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    if (self.status == 1) {
        NSString *status = self.cashingGroupModel.cashingOrder.status;
        NSString *businessType = self.cashingGroupModel.inoutcomeDetail.businessType;
        //业务类型 1：提现 2：收到车费 3：提现退款
        if ([businessType isEqualToString:@"1"]) {
            self.categaryLbl.text = @"余额提现";
            self.moneyLabel.text = [NSString stringWithFormat:@"-%@",self.cashingGroupModel.inoutcomeDetail.amount];
        }else if ([businessType isEqualToString:@"2"]){
            self.categaryLbl.text = @"收到车费";
            self.moneyLabel.text = [NSString stringWithFormat:@"+%@",self.cashingGroupModel.inoutcomeDetail.amount];
        }else if ([businessType isEqualToString:@"3"]){
            self.categaryLbl.text = @"提现退还";
            self.moneyLabel.text = [NSString stringWithFormat:@"+%@",self.cashingGroupModel.inoutcomeDetail.amount];
        }
        /** 订单状态 1：发起申请 2：处理中 :3：提现成功 4：提现失败 5：提现退还*/
        if ([status isEqualToString:@"1"]||[status isEqualToString:@"2"]) {
            self.scheduleStatus.text = @"处理中";
            self.statusImageView.image = [UIImage imageNamed:@"progress_bar_2"];
            self.middleStatus.text = @"处理中";
            self.middleStatus.textColor = GREEN_COLOR;
            self.rightLabel.hidden = YES;
            self.leftTime.text = [NSString timeWithTimeIntervalString:self.cashingGroupModel.cashingOrder.gmtCreate];
            self.middleTime.text = [NSString timeWithTimeIntervalString:self.cashingGroupModel.cashingOrder.dealingTime];
        }else if([status isEqualToString:@"3"]||[status isEqualToString:@"5"]) {
            self.scheduleStatus.text = @"提现成功";
            self.statusImageView.image = [UIImage imageNamed:@"progress_bar_3"];
            self.rightStatus.textColor = GREEN_COLOR;
            self.rightStatus.text = @"提现成功";
            self.leftTime.text = [NSString timeWithTimeIntervalString:self.cashingGroupModel.cashingOrder.gmtCreate];
            self.middleTime.text = [NSString timeWithTimeIntervalString:self.cashingGroupModel.cashingOrder.dealingTime];
            self.rightLabel.text = [NSString timeWithTimeIntervalString:self.cashingGroupModel.cashingOrder.gmtModified];
        }else if ([status isEqualToString:@"4"]){
            self.scheduleStatus.text = @"提现失败";
            self.statusImageView.image = [UIImage imageNamed:@"progress_bar_3"];
            self.rightStatus.textColor = RED_COLOR;
            self.rightStatus.text = @"提现失败";
            self.leftTime.text = [NSString timeWithTimeIntervalString:self.cashingGroupModel.cashingOrder.gmtCreate];
            self.middleTime.text = [NSString timeWithTimeIntervalString:self.cashingGroupModel.cashingOrder.dealingTime];
            self.rightLabel.text = [NSString timeWithTimeIntervalString:self.cashingGroupModel.cashingOrder.gmtModified];
        }

    }else if (self.status == 2){
        self.categaryLbl.text = @"提现退还";
        self.moneyLabel.text = [NSString stringWithFormat:@"+%@",self.cashingGroupModel.inoutcomeDetail.amount];
        self.scheduleStatus.text = @"提现退还";
        self.statusImageView.image = [UIImage imageNamed:@"progress_bar_3"];
        self.leftStatus.text = @"发起退还";
        self.leftTime.text = [NSString timeWithTimeIntervalString:self.cashingGroupModel.cashingOrder.launchRefundingTime];
        self.middleStatus.text = @"处理中";
        self.middleTime.text = [NSString timeWithTimeIntervalString:self.cashingGroupModel.cashingOrder.dealingTime];
        self.rightStatus.text = @"提现退还";
        self.rightStatus.textColor  = GREEN_COLOR;
        self.rightLabel.text = [NSString timeWithTimeIntervalString:self.cashingGroupModel.cashRefundingOrder.gmtModified];
    }else if (self.status == 3){
        self.categaryLbl.text = @"收到车费";
        self.moneyLabel.text = [NSString stringWithFormat:@"+%@",self.vehicleFeesModel.amount];
        self.moneyLabel.textColor = GREEN_COLOR;
        self.scheduleStatus.text = @"收到车费";
        self.statusImageView.image = [UIImage imageNamed:@"progress_bar_3"];
        self.leftStatus.text = @"发起收费";
        self.leftTime.text = [NSString timeWithTimeIntervalString:self.vehicleFeesModel.gmtCreate];
        self.middleStatus.text = @"处理中";
        self.middleTime.text = [NSString timeWithTimeIntervalString:self.cashingGroupModel.cashingOrder.dealingTime];
        self.rightStatus.text = @"收到车费";
        self.rightStatus.textColor  = GREEN_COLOR;
        self.rightLabel.text = [NSString timeWithTimeIntervalString:self.vehicleFeesModel.gmtCreate];
    }
     [self.tableView reloadData];
}
#pragma mark---UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SYDReturnScheduleCell *cell = [tableView dequeueReusableCellWithIdentifier:scheduleID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.status == 1) {
        
        [cell returnScheduleWithBankCard:self.cashingGroupModel.cashingOrder.bankCard createTime:[NSString timeWithTimeIntervalString:self.cashingGroupModel.cashingOrder.gmtCreate] method:@"" reason:@"" accountTime:@"" status:self.status];
    }else if (self.status == 2){
      [cell returnScheduleWithBankCard:self.cashingGroupModel.cashingOrder.bankCard createTime:self.cashingGroupModel.cashRefundingOrder.gmtCreate method:@"退回钱包" reason:self.cashingGroupModel.cashRefundingOrder.refundingReason accountTime:self.cashingGroupModel.cashRefundingOrder.gmtModified status:self.status];
    }else if (self.status == 3){
       [cell returnScheduleWithBankCard:@"" createTime:self.vehicleFeesModel.gmtCreate method:@"" reason:@"" accountTime:@"" status:self.status];
    }
    
    return cell;
}
//返回cell的的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.status == 1) {
        return 88;
    }else{
        return [self heightForRowWithReason:self.cashingGroupModel.cashRefundingOrder.refundingReason];
    }
}
//动态计算cell的高度
- (CGFloat)heightForRowWithReason:(NSString *)reason
{
    //这里只写了label的计算
    //文本的高度
    CGSize texSize = [self labelAutoCalculateRectWith:reason FontSize:13 MaxSize:CGSizeMake(200,1000)];
    //3.返回cell 的总高度
    return 172 + texSize.height;
}
/*根据传过来的文字内容、字体大小、宽度和最大尺寸动态计算文字所占用的size
 * text 文本内容
 * fontSize 字体大小
 * maxSize  size（宽度，1000）
 * return  size （计算的size）
 */
- (CGSize)labelAutoCalculateRectWith:(NSString*)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize
{
    NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
    NSDictionary* attributes =@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSParagraphStyleAttributeName:paragraphStyle.copy};
    CGSize labelSize;
    //如果是IOS6.0
    if (![text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]){
        labelSize = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
    }else{//如果系统为iOS7.0
        
        // iOS7中用以下方法替代过时的iOS6中的sizeWithFont:constrainedToSize:lineBreakMode:方法
        labelSize = [text boundingRectWithSize: maxSize options: NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
    }
    labelSize.height=ceil(labelSize.height);
    labelSize.width=ceil(labelSize.width);
    return labelSize;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(nonnull UIView *)view forSection:(NSInteger)section{
    
    view.tintColor = [UIColor clearColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
